package com.xyzwps.harbor.handler.validator;

import com.xyzwps.harbor.util.args.Args;

import java.util.Objects;
import java.util.Set;

public final class StringValidators {

    public static ValidateFunction<String> notEmpty(String message) {
        messageShouldNotBeBlank(message);
        return (s) -> {
            if (s.isEmpty()) throw new ValidationException(message);
        };
    }

    public static ValidateFunction<String> notBlank(String message) {
        messageShouldNotBeBlank(message);
        return s -> {
            if (s.isBlank()) throw new ValidationException(message);
        };
    }

    public static ValidateFunction<String> sizeShouldGE(int min, String message) {
        messageShouldNotBeBlank(message);
        Args.Integers.requireNonNegative(min);
        return s -> {
            if (s.length() < min) throw new ValidationException(message);
        };
    }

    public static ValidateFunction<String> sizeShouldLE(int max, String message) {
        messageShouldNotBeBlank(message);
        Args.Integers.requireNonNegative(max);
        return s -> {
            if (s.length() > max) throw new ValidationException(message);
        };
    }

    public static ValidateFunction<String> sizeRange(int min, int max, String geMinMessage, String leMaxMessage) {
        return Validators.sequential(
                sizeShouldGE(min, geMinMessage),
                sizeShouldLE(max, leMaxMessage)
        );
    }

    public static ValidateFunction<String> oneOf(Set<String> enums, String message) {
        messageShouldNotBeBlank(message);
        Args.Collections.requireNonEmpty(Objects.requireNonNull(enums));
        return s -> {
            if (!enums.contains(s)) throw new ValidationException(message);
        };
    }

    private static void messageShouldNotBeBlank(String message) {
        Args.Strings.requireNonBlank(Objects.requireNonNull(message));
    }
}
